package com.fengxu.springcloud.controller;

import com.fengxu.springcloud.entity.CommonResult;
import com.fengxu.springcloud.entity.Payment;
import com.fengxu.springcloud.service.PaymentFeignService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Log4j2
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/find/{id}")
    public CommonResult findById(@PathVariable("id")Long id){
        log.info("调用feign提供的find方法");
        return paymentFeignService.getPaymentById(id);
    }

    @PostMapping("/create")
    CommonResult create(@RequestBody Payment payment){
        log.info("调用feign提供的create方法");
        return paymentFeignService.create(payment);
    }

    @GetMapping("/timeout")
    public String testTimeout(){
        return paymentFeignService.timeout();
    }

    @GetMapping("/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        Map<String,Object> map = new HashMap<>();
        for (String service : services) {
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            map.put(service,instances);
        }
        return map;
    }
}
